﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace CodeGenerate.Data
{
    public class StoredProcedureUtil
    {
        public static List<StoredProcedureParam> GetStoredProcedureParam(string connStr, string procName)
        {
            List<StoredProcedureParam> paras = new List<StoredProcedureParam>();
            using (SqlConnection conn = new SqlConnection(connStr))
            {
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = conn;
                cmd.CommandText = procName;
                cmd.CommandType = CommandType.StoredProcedure;


                conn.Open();

                SqlCommandBuilder.DeriveParameters(cmd);

                foreach (SqlParameter v in cmd.Parameters)
                {
                    if (cmd.Parameters.IndexOf(v) == 0) continue;//Skip return value
                    StoredProcedureParam para = new StoredProcedureParam();
                    para.ParameterName = v.ParameterName;
                    para.SqlDbType = v.SqlDbType;
                    para.Direction = v.Direction;
                    para.DbType = v.DbType;
                    para.IsNullable = v.IsNullable;
                    para.Precision = v.Precision;
                    para.SourceColumn = v.SourceColumn;
                    para.SqlValue = v.SqlValue;
                    para.TypeName = v.TypeName;
                    para.Value = v.Value;
                    para.Size = v.Size;                    

                    paras.Add(para);
                }
            }
            return paras;

        }
    }
}
